#include "pch.hpp"
#include "KeyboardHook.hpp"
#include "KeyboardHookHelper.hpp"

#include <boostext/smart_ptr.hpp>




extern boostext::smart_ptr<KeyboardHookHelper> m_keyboardHook;




KeyboardHook::KeyboardHook(const KeyboardProc& keyboardProc)
	: m_keyboardProc(keyboardProc)
{
	if(!m_keyboardHook)
		m_keyboardHook = boostext::smart_ptr<KeyboardHookHelper>(new KeyboardHookHelper());

	// Add ourselves to the list of dispatchers
	m_keyboardHook->registerHook(this);
}
///////////////////////////////////////////////////////////////////////////////////////////////////

KeyboardHook::~KeyboardHook()
{
	// Remove ourselves from the list of dispatchers
	m_keyboardHook->unregisterHook(this);

	if(!m_keyboardHook->numHooks())
		m_keyboardHook.release();
}
///////////////////////////////////////////////////////////////////////////////////////////////////




void KeyboardHook::keyboardProc(int message, WPARAM wParam, LPARAM lParam) const
{
	// Simply invoke the function
	m_keyboardProc(message, wParam, lParam);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
